﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Model;
using AspNetCs.Bll;
using AspNetCs.Common;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.Channel_Enum;
public partial class Master_admin_friendsite : Admin_BasePage
{
    string Action = "";
    protected string TextChange = "添加";
    string whereStr = "";
    protected string Un = "";
    protected string linktype = "";
    protected FriendSite Create = new FriendSite();
    protected void Page_Load(object sender, EventArgs e)
    {
        IsExit();
        if (Request["LinkType"] != null)
        {
            linktype = Request["LinkType"].ToString();
            if (linktype + "" != "")
            {
                whereStr += " linktype=" + StringHelper.isNumber_int(Request["LinkType"].ToString()).ToString() + "  ";
            }
        }
        if (Request["Un"] != null)
        {
            Un = Request["Un"].ToString();
        }
        if (Un != "")
        {
            if (whereStr + "" != "")
            {
                whereStr += " and ";
            }
            whereStr += " SiteAdmin='" + Un + "' ";
        }


        if (Request["Action"] != null)
        {
            Action = Request["Action"].ToString().Trim().ToLower();
        }
        if (Action == "add")
        {
            this.Pl_Set.Visible = true;
            this.Pl_Manager.Visible = false;
            this.Button1.Visible = false;
            this.Button2.Visible = true;

        }






        if (!Page.IsPostBack)
        {
            bind();
        }




    }
    protected void bind()
    {
        Pager p = new Pager();
        p.Obj = Repeater1;
        p.TableName = "FriendSite";
        p.WhereStr = whereStr;
        p.Orderfld = "id";
        p.PageSize = this.AspNetPager1.PageSize;
        p.PageIndex = this.AspNetPager1.CurrentPageIndex;
        p.OrderType = "desc";
        p.ControlBind();

        this.AspNetPager1.RecordCount = p.recordcount();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        FriendSite_Model model = new FriendSite_Model();
        model.SiteName = StringHelper.HtmlEncrypt(LinkName.Text);
        model.LinkType = StringHelper.isNumber_int(types.SelectedValue);
        model.SiteUrl = StringHelper.HtmlEncrypt(LinkUrl.Text);
        model.SiteIntro = StringHelper.HtmlEncrypt(Readme.Text);
        model.LogoUrl = StringHelper.HtmlEncrypt(LinkLogo.Text);
        model.SiteAdmin = StringHelper.HtmlEncrypt(MasterName.Text);
        model.Email = StringHelper.HtmlEncrypt(Email.Text);
        if (Pwd.Text + "" != "")
        {
            model.SitePwd = EncryptString.GetEncrypt(Pwd.Text);
        }
        model.IsGood = StringHelper.isNumber_int(ontop.SelectedValue);
        model.IsCheck = StringHelper.isNumber_int(ischeck.SelectedValue);

        int Result = Create.Add(model);
        if (Result > 0)
        {
            Response.Write("<script>alert('友情链接添加成功');location.href='admin_friendsite.Aspx';</script>");
        }
        else
        {
            Response.Write("<script>alert('友情链接添加失败');history.back();</script>");
        }

        Response.End();


    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bind();
    }
    protected string LinkPic(string pic, int types)
    {
        string Result = "";

        if (types == 1)
        {
            Result = "<img src='" + pic + "' width='137' height='46' >";
        }

        return Result;

    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        LinkButton btnMdy = e.Item.FindControl("btn_modify") as LinkButton;
        LinkButton btndel = e.Item.FindControl("btn_del") as LinkButton;
        int id = StringHelper.isNumber_int(e.CommandArgument.ToString());
        if (id <= 0)
        {
            Response.Write("<script>alert(\"参数传递错误!\");history.back();</script>");
            Response.End();
        }
        if (e.CommandName == "Modify")
        {
            TextChange = "修改";
            this.Pl_Manager.Visible = false;
            this.Pl_Set.Visible = true;
            this.Button1.Visible = true;
            this.Button2.Visible = false;
            FriendSite_Model FriendSite_Info = Create.GetModel(id);
            types.SelectedValue = FriendSite_Info.LinkType.ToString();
            LinkName.Text = StringHelper.ReHtmlEncrypt(FriendSite_Info.SiteName);
            LinkUrl.Text = StringHelper.ReHtmlEncrypt(FriendSite_Info.SiteUrl);
            LinkLogo.Text = StringHelper.ReHtmlEncrypt(FriendSite_Info.LogoUrl);
            MasterName.Text = StringHelper.ReHtmlEncrypt(FriendSite_Info.SiteAdmin);
            Email.Text = StringHelper.ReHtmlEncrypt(FriendSite_Info.Email);
            Readme.Text = StringHelper.ReHtmlEncrypt(FriendSite_Info.SiteIntro);
            ontop.SelectedValue = FriendSite_Info.IsGood.ToString();
            ischeck.SelectedValue = FriendSite_Info.IsCheck.ToString();
            CurrID.Value = id.ToString();
        }

        if (e.CommandName == "delete")
        {
            int Result = Create.Delete(" and id=" + id.ToString());
            Response.Redirect("admin_friendsite.Aspx?page=" + this.AspNetPager1.CurrentPageIndex.ToString());
            Response.End();

        }
    }



    protected void Button1_Click1(object sender, EventArgs e)
    {
        FriendSite_Model model = Create.GetModel(StringHelper.isNumber_int(CurrID.Value));
        model.SiteName = StringHelper.HtmlEncrypt(LinkName.Text);
        model.LinkType = StringHelper.isNumber_int(types.SelectedValue);
        model.SiteUrl = StringHelper.HtmlEncrypt(LinkUrl.Text);
        model.SiteIntro = StringHelper.HtmlEncrypt(Readme.Text);
        model.LogoUrl = StringHelper.HtmlEncrypt(LinkLogo.Text);
        model.SiteAdmin = StringHelper.HtmlEncrypt(MasterName.Text);
        model.Email = StringHelper.HtmlEncrypt(Email.Text);
        if (Pwd.Text + "" != "")
        {
            model.SitePwd = EncryptString.GetEncrypt(Pwd.Text);
        }
        model.IsGood = StringHelper.isNumber_int(ontop.SelectedValue);
        model.IsCheck = StringHelper.isNumber_int(ischeck.SelectedValue);

        int Result = Create.update(model, " and id=" + StringHelper.isNumber_int(CurrID.Value));
        if (Result > 0)
        {
            Response.Write("<script>alert('友情链接修改成功');location.href='admin_friendsite.Aspx';</script>");
        }
        else
        {
            Response.Write("<script>alert('友情链接修改失败');history.back();</script>");
        }

        Response.End();
    }

    protected string channelType(string ToValue)
    {
        string Result = "";
        if (ToValue != "")
        {
            try
            {
                object obj = (Channel_Type)Enum.Parse(typeof(Channel_Type), ToValue);

                Result = EnumDescription.GetFieldText(obj);
            }
            catch
            {
                Result = "";
            }

            //EnumDescription.GetEnumText(typeof((Channel_Type)ToValue));


        }
        return Result;

    }

}
